const utils = require('../utils/utils');
const Service=require('egg').Service
class ProductService extends Service{
    async list(params) {
        const where = {};
        if (params.classify_id) {
          const classifyData = await this.classify();
          const ids = utils.getChildrenIds(classifyData, params.classify_id);
          where.classify_id = ids;
        }
        const products = await this.app.mysql.select('product', {
          limit: params.pageSize * 1, 
          offset: params.pageSize * (params.page - 1), 
          where,
        });
        return products;
      }
      async classify() {
        const classifyData = await this.app.mysql.select('classify');
        return utils.arrayToTree([ ...classifyData ]);
      }
}
module.exports=ProductService